//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.graphreport.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.ht.entity.Result;
import com.ht.modules.system.online.graphreport.entity.OnlGraphreportHead;
import com.ht.modules.system.online.graphreport.entity.OnlGraphreportItem;
import com.ht.modules.system.online.graphreport.entity.OnlGraphreportTemplet;
import com.ht.modules.system.online.graphreport.entity.OnlGraphreportTempletItem;
import com.ht.modules.system.online.graphreport.mapper.OnlGraphreportHeadMapper;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportApiService;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportHeadService;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportItemService;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportTempletItemService;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportTempletService;
import com.ht.modules.system.online.graphreport.util.GraphreportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OnlGraphreportApiServiceImpl extends ServiceImpl<OnlGraphreportHeadMapper, OnlGraphreportHead> implements IOnlGraphreportApiService {
    @Autowired
    private IOnlGraphreportHeadService headService;
    @Autowired
    private IOnlGraphreportItemService itemService;
    @Autowired
    private IOnlGraphreportTempletService templetService;
    @Autowired
    private IOnlGraphreportTempletItemService templetItemService;

    public OnlGraphreportApiServiceImpl() {
    }

    public Result<?> getTempletChartsData(String id) {
        Map<String, Object> resultMap = new HashMap();
        OnlGraphreportTemplet templet = (OnlGraphreportTemplet)this.templetService.getById(id);
        if (templet == null) {
            return Result.error("未找到对应实体");
        } else {
            resultMap.put("templet", templet);
            List<OnlGraphreportTempletItem> templetItems = this.templetItemService.selectByMainId(id);
            Iterator<OnlGraphreportTempletItem> iterator = templetItems.iterator();
            List<Object> groups = new ArrayList<>();
            List<Object> group = null;
            Integer groupNum = null;
            String groupStyle = null;
            String groupTxt = null;

            while(iterator.hasNext()) {
                OnlGraphreportTempletItem templetItem = (OnlGraphreportTempletItem)iterator.next();
                if (groupNum == null || !templetItem.getGroupNum().equals(groupNum)) {
                    if (group != null) {
                        groups.add(this.getGroupMap(groupNum, groupStyle, groupTxt, group));
                    }

                    group = new ArrayList<>();
                    groupNum = templetItem.getGroupNum();
                    groupStyle = templetItem.getGroupStyle();
                    groupTxt = templetItem.getGroupTxt();
                }

                String code = templetItem.getGraphreportCode();
                OnlGraphreportHead head = this.headService.queryByCode(code);
                String graphreportType = templetItem.getGraphreportType();
                if (!"normal".equals(graphreportType)) {
                    head.setGraphType(graphreportType);
                }

                Map<String, Object> chartMap = this.queryChartDataSource(head, (String)null);
                group.add(chartMap);
            }

            groups.add(this.getGroupMap(groupNum, groupStyle, groupTxt, group));
            resultMap.put("groups", groups);
            return Result.ok(resultMap);
        }
    }

    private Map<String, Object> getGroupMap(int groupNum, String groupStyle, String groupTxt, List<?> group) {
        Map<String, Object> groupMap = new HashMap();
        groupMap.put("groupNum", groupNum);
        groupMap.put("groupStyle", groupStyle);
        groupMap.put("groupTxt", groupTxt);
        groupMap.put("charts", group);
        return groupMap;
    }

    public Map<String, Object> queryChartDataSource(OnlGraphreportHead head, String paramString) {
        Map<String, Object> resultMap = new HashMap();
        resultMap.put("head", head);
        List<OnlGraphreportItem> items = this.itemService.selectByMainId(head.getId());
        resultMap.put("items", items);
        String dataType = "sql";
        if (dataType.equals(head.getDataType())) {
            String sql = head.getCgrSql();
            String conditionSql = GraphreportUtil.getSplicingConditionSql(sql, paramString);
            List<Map<?, ?>> listMap = this.headService.executeSelete(conditionSql);
            resultMap.put("data", listMap);
        }

        return resultMap;
    }
}
